* Table A7 Baseline balance comparison between full sample and balanced panel
* Sample used to study household outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* outcome vars for all years
local var1 "w1_age_head w1_head_educ w1_hasplot w1_finlit w1_wealth_index w_real_sav w_real_lent w_real_borrow w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income w_real_gifts_out w_real_gifts_in food_sufficiency_child fladder_std outlook_std control_std w_real_cons"

* outcome vars excluding vars not collected in baseline for treatment expansion group
local var9 "w1_age_head w1_head_educ w1_hasplot w1_finlit w1_wealth_index w_real_sav w_real_food_exp w_real_nonfood_exp w_real_durables_exp w_real_events_exp w_real_income food_sufficiency_child fladder_std w_real_cons "

estimates clear

* baseline demographics
gen w1_head_educ=.
gen w1_age_head=.
forvalues a=1/18 {
replace w1_head_educ=a18_c`a' if a13_c`a' == 1 & mkt_year==2005
replace w1_age_head=a14_c`a' if a13_c`a' == 1 & mkt_year==2005
replace w1_head_educ=. if w1_age_head<18 & mkt_year==2005
replace w1_age_head=. if w1_age_head<18 & mkt_year==2005
replace w1_head_educ=0 if w1_head_educ==888 
replace w1_head_educ=0 if w1_head_educ==15 | w1_head_educ==16
}


forvalues a=1/18 {
replace w1_head_educ=a18_c`a' if a13_c`a' == 1 & mkt_year==2008 & firstt_2009==1
replace w1_age_head=a14_c`a' if a13_c`a' == 1 & mkt_year==2008 & firstt_2009==1
replace w1_head_educ=. if w1_age_head<18 & mkt_year==2008 & firstt_2009==1
replace w1_age_head=. if w1_age_head<18 & mkt_year==2008 & firstt_2009==1
replace w1_head_educ=0 if w1_head_educ==888 
replace w1_head_educ=0 if w1_head_educ==15 | w1_head_educ==16
}

replace sex=0 if sex==2
rename sex male
lab var male "=1 if male"

cap drop finlit_new
egen finlit_new=rowmean(l21c l22c l23c l32c l32c l33c l34c l35c l36c l41c l42c l43c l44c)


gen w1_hasplot = hasplot if mkt_year==2005
replace w1_hasplot = hasplot if mkt_year==2008 & firstt_2009==1

gen w1_finlit = finlit_new if mkt_year==2005
replace w1_finlit = finlit_new if mkt_year==2008 & firstt_2009==1

gen w1_wealth_index = wealth_index if mkt_year==2005
replace w1_wealth_index = wealth_index if mkt_year==2008 & firstt_2009==1

*combined treat variable
gen treatment_cat=0
replace treatment_cat=1 if originaltvillage==1
replace treatment_cat=2 if firstt_2007==1
replace treatment_cat=3 if firstt_2009==1

* sum stats
qui estpost summarize `var1' if mkt_year==2005
est sto A 
qui estpost summarize `var1' if ex_post_balp & mkt_year==2005 
est sto B 
qui estpost summarize `var1' if mkt_year==2005 & treatment_cat==1
est sto E
qui estpost summarize `var1' if ex_post_balp & mkt_year==2005 & treatment_cat==1
est sto F 
qui estpost summarize `var1' if mkt_year==2005 & treatment_cat==2
est sto G 
qui estpost summarize `var1' if ex_post_balp & mkt_year==2005 & treatment_cat==2
est sto H 
qui estpost summarize `var1' if mkt_year==2008 & treatment_cat==3
est sto I
qui estpost summarize `var1' if ex_post_balp & mkt_year==2008 & treatment_cat==3
est sto J
qui estpost summarize `var1' if mkt_year==2005 & treatment_cat==0
est sto C
qui estpost summarize `var1' if ex_post_balp & mkt_year==2005 & treatment_cat==0
est sto D

esttab A B C D E F G H I J using "../output/sum-a7.csv", cells("mean(fmt(2))" replace sd(par fmt(2))) nolabel noabbrev title("Summary statistics") collabels("Mean") mtitle("Full" "Balanced") nonum replace 

*gen bal_panel_treat_dummy = bal_panel3*treatment_cat
tempvar tmp
gen `tmp' = .
foreach var of local var1 {
xi:reg `var' ex_post_balp if mkt_year == 2005, cluster(villageno)
replace `tmp' = Ftail(e(df_m),e(df_r),e(F))
label var `tmp' `var'
estpost summarize `tmp' if mkt_year == 2005
est sto A
replace `tmp' = .
xi:reg `var' ex_post_balp if originaltvillage & mkt_year == 2005, cluster(villageno)
replace `tmp' = Ftail(e(df_m),e(df_r),e(F))
label var `tmp' `var'
estpost summarize `tmp' if mkt_year == 2005
est sto C
replace `tmp' = .
xi:reg `var' ex_post_balp if firstt_2007 & mkt_year == 2005, cluster(villageno)
replace `tmp' = Ftail(e(df_m),e(df_r),e(F))
label var `tmp' `var'
estpost summarize `tmp' if mkt_year == 2005
est sto D
replace `tmp' = .
xi:reg `var' ex_post_balp if firstt_2007==0 & originaltvillage==0 & mkt_year == 2005, cluster(villageno)
replace `tmp' = Ftail(e(df_m),e(df_r),e(F))
label var `tmp' `var'
estpost summarize `tmp' if mkt_year == 2005
est sto B
replace `tmp' = .
esttab A B C D using "../output/bal-a7.csv", cells("mean(fmt(2))") label noabbrev collabels("p-value") mtitle("") noobs nonum append
estimates clear
}

foreach var of local var9 {
xi:reg `var' ex_post_balp if mkt_year == 2008 & firstt_2009==1, cluster(villageno)
replace `tmp' = Ftail(e(df_m),e(df_r),e(F))
label var `tmp' `var'
estpost summarize `tmp' if mkt_year == 2008
est sto E
replace `tmp' = .
esttab E using "../output/bal-a7-treatexp.csv", cells("mean(fmt(2))") label noabbrev collabels("p-value") mtitle("") noobs nonum append
estimates clear
}

